package arrAndstr;

/*
    238. 除自身以外数组的乘积
 */
public class T238 {

    public static void main(String[] args) {
        int[] array = new int[]{-1,1,0,-3,3};

        productExceptSelf(array);
    }

    public static int[] productExceptSelf(int[] nums) {
        int len = nums.length;
        int[] answer = new int[len];
        int[] b1 = new int[len];
        int[] b2 = new int[len];

        int temp = 1;
        for(int i=0; i<len; i++) {
            temp *= nums[i];
            b1[i] = temp;
        }

        temp = 1;
        for(int i=len-1; i>=0; i--) {
            temp *= nums[i];
            b2[i] = temp;
        }

        answer[0] = b2[1];
        answer[len-1] = b1[len-1];
        for(int i=1; i<len-1; i++) {
            answer[i] = b1[i-1] * b2[i+1];
        }
        return answer;
    }
}
